﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;

namespace TinyCinema.Data
{
    public class Schedule : Execution
    {
        public string ID { get; set; }
        public DateTime Day { get; set; }
        public TimeSpan Time { get; set; } 
        public Movie Movie { get; set; }
        public bool IsDeskAvailable { get; set; }
        int _MovieIndex;
        public int MovieIndex
        {
            get { return _MovieIndex; }
            set
            {
                Movie = lstMovie[value];
                _MovieIndex = value;
                OnPropertyChanged("this");
            }
        }
        public Room Room { get; set; }
        int _RoomIndex;
        public int RoomIndex
        {
            get { return _RoomIndex; }
            set
            {
                Room = lstRoom[value];
                _RoomIndex = value;
            }
        }
        public ObservableCollection<Movie> lstMovie
        {
            get { return Data.Movie.lstMovie; }
        }
        public ObservableCollection<Room> lstRoom
        {
            get { return Data.Room.lstRoom; }
        }
        public SchedulePrice TypePrice { get; set; }
        public Schedule() {
            ID = IDGenerate(Tables.Schedule);
            Movie = new Movie();
            Day = DateTime.Today;
            TypePrice = new SchedulePrice();
            try { RoomIndex = 0; }
            catch { }
            try { MovieIndex = 0; }
            catch { }
        }
        public void RefreshDeskStatus()
        {
            string sql = "select * from " + Tables.Ticket + " where " + TicketTable.ScheduleID + " = '" + ID + "'";
            var tbl = Excute(sql);

            foreach (var DeskRow in Room.lstDesk)
                foreach (var Desk in DeskRow)
                    for (int i = 0; i < tbl.Rows.Count;++i)
                        if (Desk.ID == tbl.Rows[i][TicketTable.DeskID] as string)
                            Desk.Status = DeskStatus.Ordered;

        }
        public Schedule(string _ID)
        {            
            ID = _ID;
            PutTableIntoDataSet(Tables.Schedule);
            var tbl = ds.Tables[Tables.Schedule];
            var Row = tbl.Select(ScheduleTable.ID + "='" + ID + "'")[0];
            Time = (TimeSpan)Row[ScheduleTable.Time];
            Day = (DateTime)Row[ScheduleTable.Day];
            TypePrice = new SchedulePrice(Row[ScheduleTable.SchedulePriceID] as string);
            if (lstRoom == null)
            {
                Room.lstRoom = new ObservableCollection<Room>();
                Room.lstRoom = Room.GetAllRoom();
            }
            foreach (var room in lstRoom)
                if (room.ID == (Row[ScheduleTable.RoomID] as string))
                {
                    RoomIndex = lstRoom.IndexOf(room);
                    break;
                }
            if (lstMovie == null)
            {
                Movie.lstMovie = new ObservableCollection<Movie>();
                Movie.lstMovie = Movie.GetAllMovie();
            }
            foreach (var Movie in lstMovie)
                if (Movie.ID == ((string)Row[ScheduleTable.MovieID]))
                {
                    MovieIndex = lstMovie.IndexOf(Movie);
                    break;
                }

            //Lấy tất cả các vé của suất chiếu
            var sql = "select * from " + Tables.Ticket + " where " + TicketTable.ScheduleID + "='" + ID + "'";
            var tblTemp = Excute(sql);
            foreach(var row in Room.lstDesk)
            {
                foreach (var Desk in row)
                {
                    var r = tblTemp.Select(TicketTable.DeskID + "='" + Desk.ID + "'");
                    Desk.Status = (r.Length > 0) ? DeskStatus.Ordered : DeskStatus.Available;
                }
            }
        }
        public static ObservableCollection<Schedule> GetAllSchedule()
        {
            ObservableCollection<Schedule> Result = new ObservableCollection<Schedule>();

            PutTableIntoDataSet(Tables.Schedule);
            var tbl = ds.Tables[Tables.Schedule];
            var Rows = tbl.Select();

            foreach (var Row in Rows)
                Result.Add(new Schedule(Row[ScheduleTable.ID] as string));
            return Result;
        }
        public static ObservableCollection<Schedule> Search(Movie movie,Room room, DateTime time)
        {
            var Cdt1 = (movie != null) ? ScheduleTable.MovieID + "='" + movie.ID + "'" : "";
            var Cdt2 = (room != null) ? ScheduleTable.RoomID + "='" + room.ID + "'" : "";
            var Cdt3 = (time != DateTime.MinValue) ? ScheduleTable.Day + "='" + time.ToShortDateString()+"'" : "";
                        
            PutTableIntoDataSet(Tables.Schedule);
            var tbl = ds.Tables[Tables.Schedule];
            var Rows1 = tbl.Select(Cdt1);
            var Rows2 = tbl.Select(Cdt2);
            var Rows3 = tbl.Select(Cdt3);

            var Result = new ObservableCollection<Schedule>();
            foreach (var Row1 in Rows1)
                foreach (var Row2 in Rows2)
                    foreach (var Row3 in Rows3)
                        if (Row1[0] as String == Row2[0] as String && Row1[0] as String == Row3[0] as String) Result.Add(new Schedule(Row1[ScheduleTable.ID] as String));
            return Result;
        }
        public bool Check()
        {
            var lstSchedule = GetAllSchedule();
            foreach(var Sch in lstSchedule)
            {
                if (Sch.Movie==null||Room==null) continue;
                if (Sch.ID != ID && Sch.Day.Year == Day.Year && Sch.Day.DayOfYear == Day.DayOfYear && (Sch.Room.ID == Room.ID || Movie.ID == Sch.Movie.ID))
                {
                    if ((Sch.Time <= Time && Sch.Time.Add(new TimeSpan(0, Sch.Movie.Duration, 0)) > Time)) return false;
                    else if (Sch.Time > Time&&Sch.Time < Time.Add(new TimeSpan(0, Movie.Duration, 0)))
                        return false;
                }
            }
            return true;
        }
        public void Save()
        {
            var tbl = ds.Tables[Tables.Schedule];
            var Rows = tbl.Select(ScheduleTable.ID + "='" + ID + "'");
            if (Rows.Length == 0)
            {
                var Row = tbl.NewRow();
                Row[ScheduleTable.ID] = ID;
                Row[ScheduleTable.Day] = Day;
                Row[ScheduleTable.Time] = Time;
                if (Movie!=null) Row[ScheduleTable.MovieID] = Movie.ID;
                if (Room!=null) Row[ScheduleTable.RoomID] = Room.ID;
                //Xác định giá suất chiếu, nếu không tìm thấy cho suất chiếu mặc định là suất chiếu đầu tiên
                var check = false;
                foreach (var TP in SchedulePrice.GetAllSchedulePrice())
                    if (TP.Day == Day.DayOfWeek && Time >= TP.StartTime && Time <= TP.EndTime&&TypePrice.ScheduleType.ID==TP.ScheduleType.ID)
                    {
                        check = true;
                        TypePrice = TP;
                        break;
                    }
                if (!check) TypePrice = SchedulePrice.GetAllSchedulePrice()[0];
                Row[ScheduleTable.SchedulePriceID] = TypePrice.ID;
                tbl.Rows.Add(Row);
            }
            else
            {
                var Row = Rows[0];
                Row[ScheduleTable.Day] = Day;
                Row[ScheduleTable.Time] = Time;
                Row[ScheduleTable.MovieID] = Movie.ID;
                Row[ScheduleTable.RoomID] = Room.ID;
                //Xác định giá suất chiếu, nếu không tìm thấy cho suất chiếu mặc định là suất chiếu đầu tiên
                var check = false;
                foreach (var TP in SchedulePrice.GetAllSchedulePrice())
                    if (TP.Day == Day.DayOfWeek && Time >= TP.StartTime && Time <= TP.EndTime && TypePrice.ScheduleType.ID == TP.ScheduleType.ID)
                    {
                        check = true;
                        TypePrice = TP;
                        break;
                    }
                if (!check) TypePrice = SchedulePrice.GetAllSchedulePrice()[0];
                Row[ScheduleTable.SchedulePriceID] = TypePrice.ID;
            }
            SaveCommand(Tables.Schedule);
        }
        public void Delete()
        {
            PutTableIntoDataSet(Tables.Schedule);
            var tbl = ds.Tables[Tables.Schedule];
            var Row = tbl.Select(ScheduleTable.ID + "='" + ID + "'")[0];
            Row.Delete();
            SaveCommand(Tables.Schedule);
        }
        public void DeskCheck()
        {
            var sql = "Select count(*) from " + Tables.Ticket + " where " + TicketTable.ScheduleID + "='" + ID + "'";
            var TicketCount = int.Parse(Excute(sql).Rows[0][0] as string);
            IsDeskAvailable = (Room.DeskCount - TicketCount == 0) ? false : true;
        }
        
    }
}
